package com.example.springbootbabyu.service.impl;

import com.example.springbootbabyu.entity.*;
import com.example.springbootbabyu.mapper.CategoryMapper;
import com.example.springbootbabyu.service.CategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 功能
 * 作者：梦天
 * 日期：2025/4/14 8:47
 */
@Service
@RequiredArgsConstructor
public class CategoryServiceImpl implements CategoryService {
    private final CategoryMapper categoryMapper;
    @Override
    public CategoryResult getSubCategories(String parentId) {
        // 1. 查询一级分类信息
        CategoryResult parentCategory = categoryMapper.selectParentCategory(parentId);
        if (parentCategory == null) {
            return null;
        }

        // 2. 查询二级分类列表
        List<CategoryDTO> subCategories = categoryMapper.selectSubCategories(parentId);

        // 3. 为每个二级分类查询商品
        subCategories.forEach(subCategory -> {
            List<GoodsDTO> goods = categoryMapper.selectGoodsByCategory(subCategory.getId());
            subCategory.setGoods(goods);
        });

        parentCategory.setChildren(subCategories);
        return parentCategory;
    }

    @Override
    public CategorySubFilter getCategoryFilter(String categoryId) {
        CategorySubFilter categorySubFilter = categoryMapper.selectCategoryFilter(categoryId);
        return categorySubFilter;
    }

    @Override
    public CategoryGoodsResult getSubCategoryGoods(SubCategoryQueryDTO queryDTO) {
        List<CategoryGoods> goods = categoryMapper.selectSubCategoryGoods(queryDTO.getCategoryId(), queryDTO.getPage()-1, queryDTO.getPageSize());
        CategoryGoodsResult categoryGoodsResult = new CategoryGoodsResult();
        categoryGoodsResult.setItems(goods);
        return categoryGoodsResult;
    }
}
